//
//  XGLoginHeaderView.swift
//  TodayNews
//
//  Created by monkey on 2020/2/5.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import SwiftTheme
import Kingfisher

class XGLoginHeaderView: UIView
{
    /// tableView偏移量
    var offsetY: CGFloat = 0 {
        didSet {
            if offsetY < 0 {
                // 放大背景图片
                let totalOffsetY = bounds.height + abs(offsetY)
                let f = totalOffsetY / bounds.height
                bgImageView.frame = CGRect(x: -(f - 1) * TodayNews_Screen_Width * 0.5, y: offsetY, width: f * TodayNews_Screen_Width, height: totalOffsetY)
            } else if abs(offsetY - 0) < 1e-6 {
                // 偏移量为0 复位
                bgImageView.frame = bounds
            }
        }
    }
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func switchThemeAction(button: UIButton) -> Void
    {
        button.isSelected = !button.isSelected
        
        TodayNewsThemeManager.shared.switchTheme(button.isSelected ? TodayNewsThemeManager.ThemeStyle.night : TodayNewsThemeManager.ThemeStyle.day)
    }
    
    /// 背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "wallpaper_profile_night"))
    private lazy var userIconImageView = UIImageView()
    private lazy var userNameLabel = UILabel(text: "测试", textColor: UIColor.white, fontSize: 20, textAlignment: .center)
}

private extension XGLoginHeaderView
{
    /// 设置界面
    func setUpUI() -> Void
    {
        backgroundColor = UIColor.white
        
        // 添加背景
        addSubview(bgImageView)
        bgImageView.frame = bounds
        
        addSubview(userIconImageView)
        userIconImageView.frame = CGRect(x: (self.width - 80) * 0.5, y: TodayNews_StatusBar_Height, width: 80, height: 80)
        if let userIcon = XGUserAccountManager.shared.userIcon {
            userIconImageView.layer.cornerRadius = 40
            userIconImageView.layer.masksToBounds = true
            userIconImageView.kf.setImage(with: URL(string: userIcon))
        }

        addSubview(userNameLabel)
        userNameLabel.frame = CGRect(x: 0, y:userIconImageView.frame.maxY + 10 , width: self.width, height: 30)
        userNameLabel.text = XGUserAccountManager.shared.screenName
                
        // 添加工具视图
        let items: [[String: Any]] = [
            ["title": "收藏","imageName": "images.myFavorite_button_normal"],
            ["title": "历史","imageName": "images.myHistory_button_normal"],
            ["title": "夜间", "selectedTitle": "白天" ,"imageName": "images.theme_button_normal","target": self, "action": #selector(switchThemeAction(button:))]
        ]
        var toolButtons = [UIButton]()
        items.forEach { (dict) in
            guard let title = dict["title"] as? String,
                  let imageName = dict["imageName"] as? String else {
                    return
            }
            
            let button = UIButton(title: title, fontSize: 17)
            button.setTitle(title, for: .normal)
            button.theme_backgroundColor = "colors.cell_background"
            button.theme_setTitleColor("colors.cell_title", forState: .normal)
            
            if let selectedTitle = dict["selectedTitle"] as? String {
                button.setTitle(selectedTitle, for: .selected)
                button.isSelected = TodayNewsThemeManager.shared.currentTheme == .night
            }
            
            button.theme_setImage(ThemeImagePicker(keyPath: imageName), forState: .normal)
            button.sizeToFit()
            button.layoutButtonWithEdgInsetStyle(.imageTop, space: 5)
            if let target = dict["target"],
                let action = dict["action"] as? Selector {
                button.addTarget(target, action: action, for: .touchUpInside)
            }
            
            toolButtons.append(button)
        }
        
        let toolStackView = UIStackView(arrangedSubviews: toolButtons)
        toolStackView.distribution = .fillEqually
        addSubview(toolStackView)
        toolStackView.frame = CGRect(x: 0, y: bounds.height - 64.0, width: bounds.width, height: 64.0)
    }
}
